package etxt2db.features;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



public class FeatureParser {
	public List<CharacterFeatureClassifier> parseFeatures(List<String> textFeatures) throws IOException{
		List<CharacterFeatureClassifier> result = new ArrayList<CharacterFeatureClassifier>();
		for(String str : textFeatures){
			if(str.toLowerCase().matches("substring\\((\\+|\\-)?[0-9]+,(\\+|\\-)?[0-9]+\\)")){
				result.add(parseSubstring(str));
			} else if (str.toLowerCase().matches("dictionary\\(\\\".+\\\"\\)")){
				result.add(parseDictionary(str));
			} else if (str.toLowerCase().matches("match\\(\\\".+\\\"\\)")){
				result.add(parseMatch(str));
			} else if (str.toLowerCase().matches("value")){
				result.add(new ValueCaseSensitiveFeatureClassifier());
			} else if (str.toLowerCase().matches("valuecaseinsensitive")){
				result.add(new ValueCaseInsensitiveFeatureClassifier());
			} else if (str.toLowerCase().matches("size")){
				result.add(new SizeFeatureClassifier());
			} else if (str.toLowerCase().matches("characterpattern")){
				result.add(new PatternFeatureClassifier());
			} else if (str.toLowerCase().matches("charactertype")){
				result.add(new CharacterTypeFeatureClassifier());
			}
		}
		
		return result;
	}
	
	private CharacterFeatureClassifier parseSubstring(String str){
		String[] division1 = str.split("\\(");
		String[] division2 = division1[1].split("\\)");
		String[] division3 = division2[0].split(",");
		if(division3[0].startsWith("+")){
			division3[0] = division3[0].substring(1);
		}
		if(division3[1].startsWith("+")){
			division3[1] = division3[1].substring(1);
		}
		return new SubstringFeatureClassifier(Integer.parseInt(division3[0]),Integer.parseInt(division3[1]));
	}
	
	private CharacterFeatureClassifier parseDictionary(String str) throws IOException{
		String[] division1 = str.split("\\\"");
		return new DictionaryFeatureClassifier(division1[1]);
	}
	
	private CharacterFeatureClassifier parseMatch(String str) throws IOException{
		String[] division1 = str.split("\\\"");
		return new RegexFeatureClassifier(division1[1]);
	}
}